package com.polaris.live.net

import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.constant.ServerConfig
import com.polaris.live.common.network.BaseNetworkApi
import com.polaris.live.common.network.interceptor.CommonParamsInterceptor
import com.polaris.live.common.network.interceptor.HttpLoggingInterceptor
import com.polaris.live.common.network.interceptor.ResponseInterceptor
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.common.util.resettableLazy
import com.polaris.live.common.util.resettableManager
import com.polaris.live.net.service.ActService
import com.polaris.live.net.service.ChatService
import com.polaris.live.net.service.GameService
import com.polaris.live.net.service.HomeService
import com.polaris.live.net.service.LiveService
import com.polaris.live.net.service.PayerService
import com.polaris.live.net.service.PusherService
import com.polaris.live.net.service.StateService
import com.polaris.live.net.service.UserService
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 网络请求实现
 *
 * @author Created by 半仙 on 2023/8/3/003 16:24
 */

val networkResettableManager = resettableManager()

//双重校验锁式-单例 封装NetApiService 方便直接快速调用简单的接口
val userService: UserService by resettableLazy(networkResettableManager) {
    NetworkApi.INSTANCE.getApi(UserService::class.java, AppConst.userUrl)
}

val payerService: PayerService by resettableLazy(networkResettableManager) {
    NetworkApi.INSTANCE.getApi(PayerService::class.java, AppConst.payerUrl)
}

val liveService: LiveService by resettableLazy(networkResettableManager) {
    NetworkApi.INSTANCE.getApi(LiveService::class.java, AppConst.liveUrl)
}

val homeService: HomeService by resettableLazy(networkResettableManager) {
    NetworkApi.INSTANCE.getApi(HomeService::class.java, AppConst.homeUrl)
}

val stateService: StateService by resettableLazy(networkResettableManager) {
    NetworkApi.INSTANCE.getApi(StateService::class.java, AppConst.stateUrl)
}

val chatService: ChatService by resettableLazy(networkResettableManager) {
    NetworkApi.INSTANCE.getApi(ChatService::class.java, AppConst.chatUrl)
}

val pusherService: PusherService by resettableLazy(networkResettableManager) {
    NetworkApi.INSTANCE.getApi(PusherService::class.java, AppConst.pusherUrl)
}

val gameService: GameService by resettableLazy(networkResettableManager) {
    NetworkApi.INSTANCE.getApi(GameService::class.java, AppConst.gameUrl)
}

val actService: ActService by resettableLazy(networkResettableManager) {
    NetworkApi.INSTANCE.getApi(ActService::class.java, AppConst.actUrl)
}
class NetworkApi : BaseNetworkApi() {
    companion object {

        val INSTANCE: NetworkApi by lazy { NetworkApi() }
    }

    override fun setHttpClientBuilder(builder: OkHttpClient.Builder): OkHttpClient.Builder {
        return builder.apply {
            addInterceptor(CommonParamsInterceptor())
            //是debug才允许打印
            addInterceptor(HttpLoggingInterceptor(ServerConfig.isDebug()))
            addInterceptor(ResponseInterceptor())
            callTimeout(15, TimeUnit.SECONDS)
        }
    }

    override fun setRetrofitBuilder(builder: Retrofit.Builder): Retrofit.Builder {
        return builder.apply {
            addConverterFactory(MoshiConverterFactory.create(JsonUtils.moshi))
        }
    }
}